package com.xinqi.modules.sys.gpt.manager.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xinqi.common.mybatis.data.service.impl.BaseServiceImpl;
import com.xinqi.common.mybatis.utils.Queries;
import com.xinqi.modules.sys.gpt.domain.ShareQuotaEntity;
import com.xinqi.modules.sys.gpt.dto.req.ShareQuotaQueryDTO;
import com.xinqi.modules.sys.gpt.manager.ShareQuotaManager;
import com.xinqi.modules.sys.gpt.mapper.ShareQuotaMapper;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 邀请规则表管理层实现
 *
 * @author: yyzhen
 * @date: 2023/05/23
 */
@Service
@RequiredArgsConstructor
public class ShareQuotaManagerImpl extends BaseServiceImpl<ShareQuotaMapper, ShareQuotaEntity> implements ShareQuotaManager {

    private LambdaQueryWrapper<ShareQuotaEntity> wrapper(ShareQuotaQueryDTO query) {
        LambdaQueryWrapper<ShareQuotaEntity> wrapper = Queries.lambda();
        Queries.accept(query.getId(), v -> wrapper.eq(ShareQuotaEntity::getId, v));
        Queries.accept(query.getShareCount(), v -> wrapper.eq(ShareQuotaEntity::getShareCount, v));
        wrapper.orderByAsc(ShareQuotaEntity::getShareCount);
        return wrapper;
    }

    @Override
    public IPage<ShareQuotaEntity> findPage(Page<ShareQuotaEntity> page, ShareQuotaQueryDTO query) {
        return this.baseMapper.selectPage(page, wrapper(query));
    }

    /**
     * 列表查询
     *
     * @param query
     * @return
     */
    @Override
    public List<ShareQuotaEntity> findList(ShareQuotaQueryDTO query) {
        return this.baseMapper.selectList(wrapper(query));
    }

    /**
     * 根据分享次数获取
     *
     * @param shareCount
     * @return
     */
    @Override
    public ShareQuotaEntity findByCount(Integer shareCount) {
        LambdaQueryWrapper<ShareQuotaEntity> wrapper = Queries.lambda();
        wrapper.eq(ShareQuotaEntity::getShareCount, shareCount)
            .last("LIMIT 1");
        return this.baseMapper.selectOne(wrapper);
    }

}
